package com.dongliu.controller;

import com.dongliu.domain.Admin;
import com.dongliu.domain.LentRecord;
import com.dongliu.domain.User;
import com.dongliu.service.AdminService;
import com.dongliu.service.BookService;
import com.dongliu.service.LentRecordService;
import com.dongliu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private LentRecordService lentRecordService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @GetMapping
    public Result getStatistics(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if(admin == null)
            return new Result(Code.LOGIN_ERR, null, "请登录后再执行操作！");
        String list[] = new String[3];
        list[0] = Integer.toString(lentRecordService.getCount());
        list[1] = Integer.toString(bookService.getCount());
        list[2] = Integer.toString(userService.getCount());
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "":"数据查询失败，请重试！";
        List<String> myList = Arrays.asList(list);
        return new Result(code, myList, msg);
    }
}
